﻿/*
	* Project	: SharePoint Dynamic Forms
	* Version	: 1.0
	* Company	: SharePoint Frontier Software Pvt Ltd. (http://www.sharepointfrontier.com)
	* Author	: Ashok Raja .T (Blog : http://www.ashokraja.me)
	* Licence	: Published under Microsoft Public License (Ms-PL)
*/

using System;

namespace SFS.SharePoint.DynamicForms.Validation
{
    public class ValidationHelper
    {
        public static string ProcessErrorMsg(SPDynamicField Field, SPDynamicFieldValidationRule Rule, string Msg)
        {
            try
            {
                return string.IsNullOrEmpty(Rule.ValidationMessage) ? Msg : Rule.ValidationMessage.Replace("[fld]", Field.DisplayName);
            }
            catch (Exception)
            {
                return "Inavalid Input Data.";
            }
        }
        public static string GetDateFromTokenAsString(string Data)
        {
            try
            {
              return  GetDateFromToken(Data).ToString("dd-MMM-yyyy");
            }
            catch 
            {
                return string.Empty;
            }
        }
        public static DateTime GetDateFromToken(string Data)
        {
            try
            {
                DateTime RuleValue = DateTime.Now;
                if (Data.Substring(0, 7).ToUpper() != "[TODAY]" && !DateTime.TryParse(Data, out RuleValue))
                {
                    throw new Exception("Not a valid date.");
                }

                RuleValue = RuleValue.Date;

                if (Data.Substring(0, 7).ToUpper() == "[TODAY]")
                {
                    RuleValue = DateTime.Now.Date;
                    if (Data.Length > 8)
                    {
                        string DateOperator = Data.Substring(7, 1);
                        if (DateOperator == "+" || DateOperator == "-")
                        {
                            string PeriodType = Data.Substring(Data.Length - 1);
                            int Period = int.Parse(Data.Substring(8, Data.Length - 9));

                            switch (PeriodType)
                            {
                                case "d":
                                    RuleValue = DateOperator == "+" ? RuleValue.AddDays(Period) : RuleValue.AddDays(Period * -1);
                                    break;

                                case "m":
                                    RuleValue = DateOperator == "+" ? RuleValue.AddMonths(Period) : RuleValue.AddMonths(Period * -1);
                                    break;

                                case "y":
                                    RuleValue = DateOperator == "+" ? RuleValue.AddYears(Period) : RuleValue.AddYears(Period * -1);
                                    break;
                                default:
                                    break;
                            }
                        }
                        else
                        {
                            if (!DateTime.TryParse(Data, out RuleValue))
                                throw new Exception("Not a valid date.");
                        }
                    }
                }
                return RuleValue;
            }
            catch
            {
                throw new Exception("Not a valid date.");
            }
        }
    }
}